package com.bawei.movie.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bawei.movie.domain.Movie;
import com.bawei.movie.service.IMovieService;
import com.bawei.movie.vo.SearchCondition;

@Controller
public class HomeController {

	@Autowired
	private IMovieService movieSerivce;

	/**
	 * 获得影片信息列表
	 * @param pageNo
	 * @param pageSize
	 * @param sortColumn
	 * @param sortType
	 * @param searchCondition
	 * @return
	 */
	@GetMapping({ "/", "/index", "/home" })
	public ModelAndView showIndexView(
			@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize, 
			@RequestParam(value = "sortColumn", required = false) String sortColumn,
			@RequestParam(value = "sortType", required = false) String sortType,
			SearchCondition searchCondition
			) {
		ModelAndView mav = new ModelAndView("movie-list");

		// 处理分页
		pageNo = pageNo != null ? pageNo : 1;
		pageSize = pageSize != null ? pageSize : 5;
		Page<Movie> page = new Page<Movie>(pageNo, pageSize);

		QueryWrapper<Movie> queryWrapper = new QueryWrapper<Movie>();

		// 处理排序
		if (sortColumn != null && sortType != null) {
			queryWrapper = "desc".equals(sortType) ? queryWrapper.orderByDesc(sortColumn)
					: queryWrapper.orderByAsc(sortColumn);
		}

		// 处理多条件查询
		if (searchCondition != null) {
			queryWrapper = searchCondition.getName() != null && !"".equals(searchCondition.getName())
					? queryWrapper.like("name", searchCondition.getName())
					: queryWrapper;

			queryWrapper = searchCondition.getFromDate() != null && searchCondition.getToDate() != null
					? queryWrapper.between("release_date", searchCondition.getFromDate(), searchCondition.getToDate())
					: queryWrapper;

			queryWrapper = searchCondition.getDirector() != null && !"".equals(searchCondition.getDirector())
					? queryWrapper.like("director", searchCondition.getDirector())
					: queryWrapper;

			queryWrapper = searchCondition.getMinPrice() != null && searchCondition.getMaxPrice() != null
					? queryWrapper.between("price", searchCondition.getMinPrice(), searchCondition.getMaxPrice())
					: queryWrapper;

			queryWrapper = searchCondition.getDecade() != null ? queryWrapper.eq("decade", searchCondition.getDecade())
					: queryWrapper;

			queryWrapper = searchCondition.getMinFilmTime() != null && searchCondition.getMaxFilmTime() != null
					? queryWrapper.between("film_time", searchCondition.getMinFilmTime(),
							searchCondition.getMaxFilmTime())
					: queryWrapper;

		}

		// 按条件分页查询数据
		IPage<Movie> moviePage = movieSerivce.page(page, queryWrapper);

		// 将查询结果设置到请求范围对象中
		mav.addObject("moviePage", moviePage);
		mav.addObject("pages", page.getPages());
		mav.addObject("sortColumn", sortColumn);
		mav.addObject("sortType", sortType);

		return mav;
	}

	/**
	 * 处理影片下架，下架
	 * @param id
	 * @return
	 */
	@GetMapping("/status")
	public ModelAndView movieUnshelve(
			@RequestParam("id") Integer id,
			@RequestParam("status") String status
			) {
		ModelAndView mav = new ModelAndView("");
		boolean result = movieSerivce.handleMovieStatusById(id, status);
		mav.setViewName("redirect:/");
		return mav;
	}
	
	@GetMapping("/remove")
	public ModelAndView removeMovie(@RequestParam("id") List<Integer> idList) {
		ModelAndView mav = new ModelAndView();
		
		movieSerivce.removeByIds(idList);
		mav.setViewName("redirect:/");
		
		return mav;
	}
}
